package com.tcm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tcm.common.enums.CachePrefix;
import com.tcm.entity.Clazz;
import com.tcm.entity.Teacher;
import com.tcm.entity.Teaching;
import com.tcm.entity.Term;
import com.tcm.entity.bo.UserCacheBo;
import com.tcm.entity.vo.BookListResult;
import com.tcm.entity.vo.ClazzListResult;
import com.tcm.entity.vo.HomeTeachingCourseResult;
import com.tcm.entity.vo.HomeTeachingTermResult;
import com.tcm.service.*;
import com.tcm.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页业务接口实现类
 * @author Guqier
 * @version 1.0
 * @date 2023/8/2 14:59
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeachingService teachingService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private TermService termService;

    @Autowired
    private BookService bookService;

    /**
     * 获取首页教师教学计划
     * @param currUserId 当前登录用户ID
     * @return 教师教学计划
     */
    @Override
    public List<HomeTeachingTermResult> getTeacherTeachingList(Long currUserId){
        // 判断是否是教师
        if (!roleService.isTeacher(currUserId)) {
            return null;// 如果不是教师则不展示数据
        }
        // 获取当前登录用户信息
        UserCacheBo userCacheBo = redisUtil.getCacheObject(CachePrefix.PC_USER_TOKEN.getCacheKey(String.valueOf(currUserId)));
        Teacher teacher = teacherService.getTeacherByAccount(userCacheBo.getAccount());
        LambdaQueryWrapper<Teaching> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teaching::getTeacherId, teacher.getId());
        // 获取所有教学计划
        List<Teaching> teachings = teachingService.list(queryWrapper);

        // 获取班级数据
        LambdaQueryWrapper<Clazz> clazzLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Long> clazzIdList = teachings.stream().map(Teaching::getClazzId).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(clazzIdList)) {
            clazzLambdaQueryWrapper.apply("1 != 1");
        }else {
            clazzLambdaQueryWrapper.in(Clazz::getId, clazzIdList);
        }
        List<Clazz> clazzList = clazzService.list(clazzLambdaQueryWrapper);
        //获取学期数据
        LambdaQueryWrapper<Term> termLambdaQueryWrapper = new LambdaQueryWrapper<>();
        termLambdaQueryWrapper.in(Term::getId, teachings.stream().map(Teaching::getTermId).distinct().collect(Collectors.toList()));
        List<Term> termList = termService.list(termLambdaQueryWrapper);
        // 获取课程数据
        List<BookListResult> courseList = bookService.getCourseList();

        // 构造结果集
        // 将学期根据时间降序排序
        Comparator<Term> comparator = (o1, o2) -> o2.getStartTime().compareTo(o1.getStartTime());
        termList.sort(comparator);

        return termList.stream().map(term -> {
            HomeTeachingTermResult homeTeachingTermResult = new HomeTeachingTermResult();
            BeanUtils.copyProperties(term, homeTeachingTermResult);

            // 查找当前学期的课程
            List<Long> courseIds = teachings.stream()
                    .filter(teaching -> teaching.getTermId().equals(term.getId()))
                    .map(Teaching::getBookId)
                    .collect(Collectors.toList());

            List<HomeTeachingCourseResult> homeTeachingCourseResults = courseList.stream()
                    .filter(course -> courseIds.contains(course.getId()))
                    .map(course -> {
                        HomeTeachingCourseResult homeTeachingCourseResult = new HomeTeachingCourseResult();
                        BeanUtils.copyProperties(course, homeTeachingCourseResult);

                        // 查找当前学期、当前课程所教班级
                        List<Long> clazzIds = teachings.stream()
                                .filter(teaching -> teaching.getTermId().equals(term.getId()) && teaching.getBookId().equals(course.getId()))
                                .map(Teaching::getClazzId)
                                .collect(Collectors.toList());

                        List<ClazzListResult> clazzListResults = clazzList.stream()
                                .filter(clazz -> clazzIds.contains(clazz.getId()))
                                .map(clazz -> {
                                    ClazzListResult clazzListResult = new ClazzListResult();
                                    BeanUtils.copyProperties(clazz, clazzListResult);
                                    return clazzListResult;
                                })
                                .collect(Collectors.toList());
                        homeTeachingCourseResult.setClazzs(clazzListResults);
                        return homeTeachingCourseResult;
                    })
                    .collect(Collectors.toList());

            homeTeachingTermResult.setCourses(homeTeachingCourseResults);
            return homeTeachingTermResult;
        }).collect(Collectors.toList());
    }

}
